package com.xk857.article.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xk857.entities.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 评论信息表 Mapper 接口
 *
 * @author CV大魔王
 * @since 2021-02-21
 */
public interface CommentMapper extends BaseMapper<Comment> {

    /**
     * 通过文章id级联查询所有评论
     *
     * @param articleId 文章id
     * @return 所有评论
     */
    @Select("SELECT * FROM xk857_comment WHERE parent_id = -1 AND article_id = #{articleId} ORDER BY create_date DESC")
    @Results({
            @Result(column = "id", property = "children", javaType = List.class, many = @Many(select = "com.xk857.article.mapper.CommentMapper.findChildrenByCommentId")),
            @Result(column = "id", property = "id")
    })
    List<Comment> findByArticleId(@Param("articleId") String articleId);


    /**
     * 根据评论id查询子评论
     * @param id 评论id
     * @return 所有子评论集合
     */
    @Select("SELECT * FROM xk857_comment WHERE parent_id = #{id}")
    @Results({
            @Result(column = "id", property = "children", javaType = List.class, many = @Many(select = "com.xk857.article.mapper.CommentMapper.findChildrenByCommentId")),
            @Result(column = "id", property = "id")
    })
    List<Comment> findChildrenByCommentId(@Param("id") String id);


    /**
     * 查询今日评论数量
     * @return
     */
    @Select("select count(id) from xk857_comment where to_days(create_date) = to_days(now())")
    Integer searchCountDayNum();


    /**
     * 查询昨日评论数量
     * @return
     */
    @Select("SELECT count(id) FROM xk857_comment WHERE TO_DAYS(NOW( )) - TO_DAYS(create_date) <= 1")
    Integer searchNumberOfArticlesInYesterdaySDay();


    /**
     * 查询近七天评论数量
     * @return
     */
    @Select("SELECT count(id) FROM xk857_comment where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_date)")
    Integer searchNumberOfArticlesInWeek();
}
